package com.oocl.server.parser.impl;



import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.SAXOutputter;
import org.jdom.output.XMLOutputter;

import com.oocl.server.parser.HtmlParser;
import com.oocl.server.pojo.User;
import com.oocl.server.util.Logger;

public class HtmlParserImpl implements HtmlParser {

	private List<User> userList;
	private long curTime;
	private long serverOpenTime;
	private DateFormat format = new SimpleDateFormat("HH:mm:ss");
	
	public HtmlParserImpl setOnlineList(List<User> userList) {
		this.userList = userList;
		return this;
	}

	// 设置当前时间
	public HtmlParserImpl setCurTime(long curTime) {
		this.curTime = curTime;
		return this;
	}

	// 设置服务器开启了多长时间
	public HtmlParserImpl setServerOpenTime(long serverOpenTime) {
		this.serverOpenTime = serverOpenTime;
		return this;
	}

	@Override
	public void genericHtml(boolean isOpened, OutputStream out) {

		// /首先，根据当前服务端的状态，返回不同的消息。
		XMLOutputter outputter = new XMLOutputter();
		Element root = null;
		if (isOpened) {
			// 传一个有用户列表的html
			root = dealWithOpen();
		} else {
			// 传一个 只有两个Button的html
			root = dealWithClose();
		}

		try {
			Logger.log("回传响应给浏览器");
			out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
			outputter.output(root, out);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Element dealWithOpen() {
		Element root = new Element("html");
		
		Element head = new Element("html");
		Element meta = new Element("meta");
		meta.setAttribute("http-equiv","Content-Type");
		meta.setAttribute("content","text/html; charset=utf-8");
		head.addContent(meta);
		root.addContent(head);
		
		Element body = new Element("body");
	 
		body.setAttribute("margin", "auto");
		
		Element div = new Element("div");
//		div.setAttribute("width","100%");
		div.setAttribute("style","border-radius:20px 20px 20px 20px;border:2px solid #000;width:auto; height:auto; margin:auto;background-color:#d2e9ff;padding:30px");
		
		Element formA = new Element("form");
		formA.setAttribute("action","http://localhost:9000/");
		formA.setAttribute("method","POST");
		formA.setAttribute("style","text-align:center;");
		
		Element btnOpen = new Element("input");
		
		btnOpen.setAttribute("type","submit");
		btnOpen.setAttribute("value","启动");
		btnOpen.setAttribute("name","open");
		Element labelOpen = new Element("label");
		labelOpen.setAttribute("name","action");
		labelOpen.setAttribute("value","open");
		btnOpen.setAttribute("style","margin:8px;font-size:24px;border-radius:6px 6px 6px 6px;border:1px solid #000;padding:8px;background-color:#fff");
		formA.addContent(labelOpen);
		formA.addContent(btnOpen);

		
		
		Element formB = new Element("form");
		formB.setAttribute("action","http://localhost:9000/");
		formB.setAttribute("method","POST");
		Element btnClose = new Element("input");
		
		btnClose.setAttribute("type","submit");
		btnClose.setAttribute("value","关闭");
		btnClose.setAttribute("name","close");
		btnClose.setAttribute("style","margin:8px;font-size:24px;border-radius:6px 6px 6px 6px;border:1px solid #000;padding:8px;background-color:#fff");
		formB.addContent(btnClose);
		formB.setAttribute("style","text-align:center;");
		
		Element serverStatus = new Element("h1");
		serverStatus.setText("服务器状态： 开启");
		serverStatus.setAttribute("style", "color:blue;text-align:center;");
		serverStatus.setAttribute("margin", "auto");
		
		
		Element timeDiv = new Element("h2");
		timeDiv.setText("聊天服务器已运行："+ format.format(this.curTime - TimeZone.getDefault().getRawOffset()));
		timeDiv.setAttribute("style", "color:green;text-align:center;");
		
		
		
		Element userTable = new Element("table");
		userTable.setAttribute("style","border-radius:20px 20px 20px 20px;border:2px solid #000;width:auto; height:auto; margin:auto;background-color:#fff;padding:30px");
		Element userListHead = new Element("tr");
		Element userNameLb = new Element("th");
		Element userIsOnlineLb = new Element("th");
		userNameLb.setText("用户名");
		userIsOnlineLb.setText("状态");
//		
//		userNameLb.setAttribute("style", "width:120px");
//		userIsOnlineLb.setAttribute("style", "width:120px");
		userListHead.addContent(userNameLb);
		userListHead.addContent(userIsOnlineLb);
		userTable.addContent(userListHead);
		
		userTable.setAttribute("align","center");
		
		for(User item: this.userList){
			Element line = new Element("tr");
			Element userName = new Element("td");
			userName.setAttribute("style","color:#7d7dff;font-weight:bold;text-align:center;padding:10px 20px;");
			userName.setText(item.getUserName());
			
			Element userIsOnline = new Element("td");
			userIsOnline.setText((item.isOnline())?"在线":"不在线");
			if(item.isOnline())
				userIsOnline.setAttribute("style","background-color:#00e3e3;text-align:center; padding:10px 30px;");
			else
				userIsOnline.setAttribute("style","background-color:#ff9797;text-align:center; padding:10px 30px;");
			
			
			line.addContent(userName);
			line.addContent(userIsOnline);
			userTable.addContent(line);
		}
		
		div.addContent(formA);
		div.addContent(formB);
		div.addContent(serverStatus);
		div.addContent(timeDiv);
		div.addContent(userTable);

		body.addContent(div);
		root.addContent(body);
		
		return root;
	}

	private Element dealWithClose() {
		Element root = new Element("html");
		
		Element head = new Element("html");
		Element meta = new Element("meta");
		meta.setAttribute("http-equiv","Content-Type");
		meta.setAttribute("content","text/html; charset=utf-8");
		head.addContent(meta);
		root.addContent(head);
		
		Element body = new Element("body");

		
		Element div = new Element("div");
//		div.setAttribute("width","100%");
		div.setAttribute("style","border-radius:20px 20px 20px 20px;border:2px solid #000;width:auto; height:auto; margin:auto;background-color:#d2e9ff;padding:30px");
	
		
		Element formA = new Element("form");
		formA.setAttribute("action","http://localhost:9000/");
		formA.setAttribute("method","POST");
		formA.setAttribute("style","text-align:center;");
		Element btnOpen = new Element("input");
		
		btnOpen.setAttribute("style","font-size:24px;border-radius:6px 6px 6px 6px;border:1px solid #000;padding:8px;background-color:#fff;margin:8px;");
		btnOpen.setAttribute("type","submit");
		btnOpen.setAttribute("value","启动");
		btnOpen.setAttribute("name","open");
		formA.addContent(btnOpen);

		
		Element formB = new Element("form");
		formB.setAttribute("action","http://localhost:9000/");
		formB.setAttribute("method","POST");
		Element btnClose = new Element("input");
		
		btnClose.setAttribute("style","font-size:24px;border-radius:6px 6px 6px 6px;border:1px solid #000;padding:8px;background-color:#fff;margin:8px;");
		btnClose.setAttribute("type","submit");
		btnClose.setAttribute("value","关闭");
		btnClose.setAttribute("name","close");
		formB.addContent(btnClose);
		formB.setAttribute("style","text-align:center;");
		
		Element serverStatus = new Element("h1");
		serverStatus.setText("服务器状态： 关闭");
		serverStatus.setAttribute("style", "color:red;text-align:center;");
		
		div.addContent(formA);
		div.addContent(formB);
		div.addContent(serverStatus);
		
		body.addContent(div);
		root.addContent(body);
		
		return root;
	}

	@Override
	public void send500Error(String errorMsg, OutputStream out) {
		// /首先，根据当前服务端的状态，返回不同的消息。
		XMLOutputter outputter = new XMLOutputter();

		Element root = new Element("html");

		Element head = new Element("head");
		Element meta = new Element("meta");
		meta.setAttribute("http-equiv", "Content-Type");
		meta.setAttribute("content", "text/html; charset=utf-8");
		head.addContent(meta);
		root.addContent(head);

		Element body = new Element("body");

		Element div = new Element("div");
		// div.setAttribute("width","100%");
		div.setAttribute("style",
		"border-radius:20px 20px 20px 20px;border:2px solid #000;width:auto; height:auto; margin:auto;background-color:#d2e9ff;padding:30px");

		Element serverStatus = new Element("h1");
		serverStatus.setText("服务端异常： " + errorMsg);
		serverStatus.setAttribute("style", "color:red;text-align:center;");

		div.addContent(serverStatus);

		body.addContent(div);
		root.addContent(body);


		try {
		Logger.log("9000端口：回传响应给浏览器;");
		out.write("HTTP/1.1 200 OK\r\n\r\n.getBytes()".getBytes());
		outputter.output(root, out);
		out.close();
		} catch (IOException e) {
		Logger.log("9000端口：回传500错误时， 出现异常：+e.getMessage()");
		} 


		
	}

}
